package com.yilife.kdys.controller;

import com.yilife.kdys.JSONResult;
import com.yilife.kdys.RespStatusCode;
import com.yilife.kdys.model.request.RegisterRequest;
import com.yilife.kdys.service.UserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by wuganlin on 2017/2/8.
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    @ResponseBody
    public JSONResult register(HttpServletRequest request) {
        JSONResult result = new JSONResult();
        RegisterRequest req = new RegisterRequest();
        try {
            req.fill(request);
        } catch (Exception e) {
            e.printStackTrace();
            result.setErrorcode(RespStatusCode.PARAMS_ERROR);
            return result;
        }
        result = userService.register(req);
        return result;
    }

    @RequestMapping("/getUserInfo")
    @ResponseBody
    public JSONResult getUserInfo(HttpServletRequest request) {
        JSONResult result = new JSONResult();
        int memberId = 0;
        try {
            memberId = Integer.parseInt(request.getParameter("member_id"));
        } catch (NumberFormatException e) {
            result.setErrorcode(RespStatusCode.PARAMS_ERROR);
            result.setErrormsg("PARAMS ERROR");
            return result;
        }
        result = userService.getUserInfo(memberId);
        return result;
    }

}
